// all controls are gauges, rotate -1 simply reduces the angular
// acceleration, thrust +1 increases the forward accelleration.
function Ship( x, y, id ) {
  var id = id;
  var r = ShipR;
  // these are in the parent reference frame
  var x = x;
  var y = y;
  var dx = 0;
  var dy = 0;
  var ddx = 0;
  var ddy = 0;
  var a = 0;// a is for angle
  var da = 0;
  var dda = 0;
  var thrustLevel = 0;

  // set up the drawing
  var graphic = document.createElementNS("http://www.w3.org/2000/svg", "g");
  var sprite = document.createElementNS("http://www.w3.org/2000/svg", "g");
  graphic.appendChild( sprite );

  var circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
  sprite.appendChild( circle );
  var line = document.createElementNS("http://www.w3.org/2000/svg", "line");
  sprite.appendChild( line );
  var aftThrust = document.createElementNS("http://www.w3.org/2000/svg", "line");
  sprite.appendChild( aftThrust );
  var foreThrust = document.createElementNS("http://www.w3.org/2000/svg", "line");
  sprite.appendChild( foreThrust );
  var portThrust = document.createElementNS("http://www.w3.org/2000/svg", "line");
  sprite.appendChild( portThrust );
  var starboardThrust = document.createElementNS("http://www.w3.org/2000/svg", "line");
  sprite.appendChild( starboardThrust );

//   sprite.setAttribute( "stroke", "white" );
  sprite.setAttribute( "fill", "black" );
  circle.setAttribute( "cx", "0" );
  circle.setAttribute( "cy", "0" );
  circle.setAttribute( "r", r );
  line.setAttribute( "stroke", "white" );
  line.setAttribute( "x1", "0" );
  line.setAttribute( "y1", "0" );
  line.setAttribute( "x2", r );
  line.setAttribute( "y2", "0" );
  aftThrust.setAttribute( "x1", -r );
  aftThrust.setAttribute( "y1", "0" );
  aftThrust.setAttribute( "x2", -r );
  aftThrust.setAttribute( "y2", "0" );
  aftThrust.setAttribute( "stroke", "orange" );
  aftThrust.setAttribute( "stroke-width", 0 );
  foreThrust.setAttribute( "x1", r );
  foreThrust.setAttribute( "y1", "0" );
  foreThrust.setAttribute( "x2", r );
  foreThrust.setAttribute( "y2", "0" );
  foreThrust.setAttribute( "stroke", "orange" );
  foreThrust.setAttribute( "stroke-width", 0 );
  portThrust.setAttribute( "x1", "0" );
  portThrust.setAttribute( "y1", -r );
  portThrust.setAttribute( "x2", "0" );
  portThrust.setAttribute( "y2", -r );
  portThrust.setAttribute( "stroke", "orange" );
  portThrust.setAttribute( "stroke-width", 3 );
  starboardThrust.setAttribute( "x1", "0" );
  starboardThrust.setAttribute( "y1", r );
  starboardThrust.setAttribute( "x2", "0" );
  starboardThrust.setAttribute( "y2", r );
  starboardThrust.setAttribute( "stroke", "orange" );
  starboardThrust.setAttribute( "stroke-width", 3 );

  // private methods
  var rotateTo = function( t ) {
	  sprite.setAttribute( "transform", "rotate("+t+")" );
  }
  var moveTo = function( x, y ) {
	  graphic.setAttribute( "transform", "translate("+x+","+y+")" );
  }
  var drawThrust = function( thrust ) {
	  if ( thrust >= 0 ){
		  foreThrust.setAttribute( "stroke-width", 0 );
		  aftThrust.setAttribute( "stroke-width", 3 );
		  aftThrust.setAttribute( "x2", -r - (thrust *10 / TW) );
	  } else {
		  aftThrust.setAttribute( "stroke-width", 0 );
		  foreThrust.setAttribute( "stroke-width", 3 );
		  foreThrust.setAttribute( "x2", r - (thrust *10 / TW) );
	  }
	  portThrust.setAttribute( "x2", -dda*10 / AW );
	  starboardThrust.setAttribute( "x2", dda*10 / AW);
  }
  var commitThrust = function() { 
	  ddx = thrustLevel * Math.cos( Math.PI*a/(180) );
	  ddy = thrustLevel * Math.sin( Math.PI*a/(180) );
  }
  // Privileged / exposed methods
  this.getGraphic = function() { return graphic; }
  this.updateState = function( ds ) {
	  commitThrust();
	  dx += ds*ddx;
	  dy += ds*ddy;
	  x += ds*dx;
	  y += ds*dy;
	  da += ds*dda;
	  a += ds*da;
  }
  this.updateView = function() {
	  rotateTo( a );
	  moveTo( x, y );
	  drawThrust( thrustLevel, dda );
  }
  this.rotate = function( inc ) {
	  dda += inc*AW;
  }
  this.thrust = function( inc ) {
	  if ( inc == 0 ){
		  thrustLevel = 0;
		  dda = 0;
	  } else {
		  thrustLevel += inc*TW;
	  }
  }
}

// external / control functions
// Ship.prototype.networkSubscribe = function( cometMsg ) {
// 	this.rotateTo( cometMsg.data.a );
// 	this.moveTo( cometMsg.data.x, cometMsg.data.y );
// };
// Ship.prototype.updateNetwork = function() {
// 	// this should be somewhere else.. but just trying
// 	//	dojox.cometd.publish( "/galactic/move", { a:a, x:x, y:y } );
// };
